package class1.s2022350143028.service.Impl;

import class1.s2022350143028.data.GoodsData;
import class1.s2022350143028.model.GoodsModel;

import java.util.Random;
import java.util.Scanner;

public class GoodssxyServiceImpl {
    GoodsData goodsData =new GoodsData();

//    /**
//     * 商品录入业务逻辑，返回bool处理结果
//     * @return boolean
//     */

    public boolean addGoods() {
        //主函数
        GoodsModel goods = new GoodsModel();
        Scanner scanner = new Scanner(System.in);

        System.out.println("开始录入商品：");

        System.out.println("请输入商品名称：");
        String goodsname = scanner.next();
        goods.setGoodName(goodsname);

        System.out.println("请输入商品规模：");
        String scale = scanner.next();
        goods.setSpecifications(scale);

        System.out.println("请输入商品价格：");
        int price = scanner.nextInt();
        goods.setPrice(price);

        System.out.println("请输入商品库存数量：");
        int number = scanner.nextInt();
        goods.setinventory(number);

        String head="G";
        Random rand =new Random();
        int number1 = rand.nextInt(9000) + 1000;
        String s=String.valueOf(number1);
        String goodId=head+s;
        goods.setGoodId(goodId);

        if (goodsData.save(goods)) {
            return true;
        } else {
            return false;
        }
    }

    //    /**
//     * 商品编辑业务逻辑，返回bool处理结果
//     * @return boolean
//     */
    public boolean editGoods(){

        GoodsModel goods = new GoodsModel();
        Scanner scanner = new Scanner(System.in);

        System.out.println("开始编辑商品：");

        System.out.println("请输入需要修改商品的商品编号：");
        String goodID = scanner.next();
        goods.setGoodId(goodID);

        System.out.println("请输入修改后的商品名称：");
        String goodsname = scanner.next();
        goods.setGoodName(goodsname);

        System.out.println("请输入修改后的商品规模：");
        String scale = scanner.next();
        goods.setSpecifications(scale);

        System.out.println("请输入修改后的商品价格：");
        int price = scanner.nextInt();
        goods.setPrice(price);

        System.out.println("请输入修改后的商品库存数量：");
        int number = scanner.nextInt();
        goods.setinventory(number);


        if (goodsData.Update(goods)) {
            System.out.println("修改成功！");
            return true;
        } else {
            System.out.println("修改失败！");
            return false;
        }
    }

    /**
     * 商品删除业务逻辑，返回bool处理结果
     * @param primaryKey 根据商品编号删除
     * @return boolean
     */
    public boolean delete(String primaryKey){
        if(goodsData.delete(primaryKey)){
            System.out.println("删除成功！");
            return true;
        }
        else return false;
    }

    //    /**
//     * 商品库存增加或减少，返回bool处理结果
//     * @param goodsId 商品编号
//     * @param number 根据商品编号管理库存
//     * @return boolean
//     */
    public boolean setInventory(){
        System.out.println("请输入需要修改库存的商品编号：");

        Scanner scanner = new Scanner(System.in);
        String goodsId= scanner.next();

        System.out.println("请输入需要增加或减少的库存数量:");
        int number = scanner.nextInt();

        if (goodsData.SetInventory(goodsId,number)) {
            System.out.println("修改成功！");
            return true;
        }
        else {
            System.out.println("修改失败！");
            return false;
        }

    }

    /**
     * 查询商品 模糊查询
     */

    public void getGood(){
        System.out.print("请输入需要查询的商品名称：");
        Scanner scanner = new Scanner(System.in);
        String query = scanner.next();
        if(!goodsData.Get(query)){
            System.out.println("查无此商品！");
        }
        return;
    }

}
